// component/mediciaList/mediciaList.js
const { formatDate } = require('../../utils/date')
const { addRemind } = require('../../api/index')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    formate:{
      type:Object,
      default:{
        //名称
        drugName:'',
        // 吃药时间
        beginTime:new Date().getTime(),
        // 吃药次数
        drugNumber:'3',
        // 吃药间隔
        drugDuring:'3',
        // 吃药天数
        drugDays:'7',
        // 吃药频率/提醒日间隔
        drugSpace:'0',
        // 单位
        drugUnit:'片',
        // 剂量
        drugDose:"3",
        // 备忘录
        note:'123'
    },
    }
  },

  data:{
    // 药品名称
    mdName:'',
    showTimePick:false,
    // 时间选择器设置
    minDate: new Date().getTime(),
    maxDate: new Date(2030, 10, 1).getTime(),
    currentDate: new Date().getTime(),
    numbers:[1,2,3,4,5,6,7,8,9,10],
    showNumberPick:false,
    showIntervalPick:false,
    intervals:[1,2,3,4,5,6,7,8,9,10],
    showDayPick:false,
    days:[1,2,3,4,5,6,7,8,9,10,11,12,13],
    showFrequencyPick:false,
    frequencys:['0','1','2','3','4','5','6','7','8','9','10'],
    showUnitPick:false,
    units:['片','克','毫升','毫克'],
  },
  onchange(e){
    this.setData({formate:{note:e.detail.value}})
  },
  /**
   * @param showXXXPick 打开弹出层
   * @param XXXPickConfirm 选择次数并且关闭弹出层
   * @param XXXPickCancel 关闭弹出层
   */
  showTimePick(){
    this.setData({showTimePick:true})
  },
  onClose(){
    this.setData({showTimePick:false})
  },
  timePickConfirm(event){
    let startTime = formatDate(event.detail)
    this.setData({
      startTime,
      showTimePick:false
    });
  },
  timePickCancel(){
    this.setData({showTimePick:false})
  },
  /**
   * 吃药次数相关函数设置
   */
  showNumberPick(){
    this.setData({showNumberPick:true})
  },
  numberPickConfirm(event){
    const { value, index } = event.detail
    this.setData({
      number:value,
      showNumberPick:false
    })
  },
  numberPickCancel(){
    this.setData({showNumberPick:false})
  },
  /**
   * 吃药间隔相关设置
   */
  showIntervalPick(){
    this.setData({showIntervalPick:true})
  },
  intervalPickConfirm(event){
    const { value, index } = event.detail
    this.setData({
      interval:value,
      showIntervalPick:false
    })
  },
  intervalPickCancel(){
    this.setData({showIntervalPick:false})
  },
  // 吃药天数相关函数
  showDayPick(){
    this.setData({showDayPick:true})
  },
  dayPickConfirm(event){
    const { value, index } = event.detail
    this.setData({
      day:value,
      showDayPick:false
    })
  },
  dayPickCancel(){
    this.setData({showDayPick:false})
  },
  // 吃药频率相关函数
  showFrequencyPick(){
    this.setData({showFrequencyPick:true})
  },
  frequencyPickConfirm(event){
    const { value, index } = event.detail
    this.setData({
      frequency:value,
      showFrequencyPick:false
    })
  },
  frequencyPickCancel(){
    this.setData({showFrequencyPick:false})
  },
  // 单位
  showUnitPick(){
    this.setData({showUnitPick:true})
  },
  unitPickConfirm(event){
    const { value, index } = event.detail
    this.setData({
      unit:value,
      showUnitPick:false
    })
  },
  unitPickCancel(){
    this.setData({showUnitPick:false})
  },
  // 保存按钮,保存后上传信息
  save(){
    // let that = this.data
    // let data = {
    //   drugName:that.mdName,
    //   beginTime:that.startTime,
    //   drugDays:that.day,
    //   drugDuring:that.interval,
    //   drugSpace:that.frequency,
    //   drugNumber:that.number,
    //   drugUnit:that.unit,
    //   drugDose:that.dose,
    //   note:that.textarea
    // }
    // 发送新添信息
    addRemind(this.properties.formate)
      .then(res=>{
        console.log(res)
      })
  }
})
